package com.credithc.customers.manager.controller;

import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.message.Message;
import com.credithc.baseserv.core.message.MessageFactory;
import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.customers.base.codes.CustomerCodes;
import com.credithc.customers.manager.ro.CompanyCustomerInfoRo;
import com.credithc.customers.manager.ro.CustomerInfoRo;
import com.credithc.customers.manager.ro.PreCustomerInfoRo;
import com.credithc.customers.manager.service.ICustomerInfoService;
import com.credithc.customers.manager.vo.CompanyCustomerInfoVo;
import com.credithc.customers.manager.vo.CustomerInfoVo;
import com.credithc.customers.manager.vo.PreCustomerInfoVo;
import com.google.common.base.Splitter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title:
 * @Author: Denghx
 * @Date: 2018/12/17 11:09
 * @Description: com.credithc.customers.manager.controller
 * @Version: V1.0.0
 */
@RestController
@RequestMapping("/manager/yzt/v1")
public class CustomerManagerController {
    @Autowired
    MessageFactory messageFactory;

    @Autowired
    ICustomerInfoService customerInfoService;

    @GetMapping("/preCustomer")
    public Message getPreCustomer(@RequestParam(name = "contactType", required = false) String contactType,
                                  @RequestParam(name = "contactInfo", required = false) String contactInfo,
                                  @RequestParam(name = "preCustomerNo", required = false) String preCustomerNo,
                                  @RequestParam(name = "createTimeStart", required = false) String createTimeStart,
                                  @RequestParam(name = "createTimeEnd", required = false) String createTimeEnd,
                                  @RequestParam(name = "updateTimeStart", required = false) String updateTimeStart,
                                  @RequestParam(name = "updateTimeEnd", required = false) String updateTimeEnd,
                                  @RequestParam(name = "pageNo", defaultValue = "0", required = false) Integer pageNo,
                                  @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                                  @RequestParam(name = "subSystemSign", required = false) String subSystemSign) {
        PreCustomerInfoRo ro = new PreCustomerInfoRo();
        ro.setContactType(contactType);
        ro.setContactInfo(contactInfo);
        ro.setPreCustomerNo(preCustomerNo);
        ro.setCreateTimeStart(createTimeStart);
        ro.setCreateTimeEnd(createTimeEnd);
        ro.setUpdateTimeStart(updateTimeStart);
        ro.setUpdateTimeEnd(updateTimeEnd);
        ro.setPageNo(pageNo);
        ro.setPageSize(pageSize);
        if (StringUtils.isNotBlank(createTimeStart) && StringUtils.isNotBlank(createTimeEnd)) {
            if (createTimeStart.compareTo(createTimeEnd) >= 0) {
                return messageFactory.getInstance(CustomerCodes.PARAM_VALIDATE_ERROR, "结束时间必须大于起始时间");
            }
        }
        if (StringUtils.isNotBlank(subSystemSign)) {
            List<String> systemSigns = Splitter.on(",").trimResults().splitToList(subSystemSign);
            ro.setSubSystemSigns(systemSigns);
        }
        // PageInfo<PreCustomerInfoVo> list = customerInfoService.selectAllPre(ro);
        PageInfo<PreCustomerInfoVo> infoList = customerInfoService.selectAllPreV2(ro);

        Map<String, Object> pageInfo = new HashMap<>();
        pageInfo.put("pageInfo", infoList);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, pageInfo);
    }

    @GetMapping("/customer")
    public Message getCustomer(@PathParam("customerNo") String customerNo, @PathParam("customerName") String customerName, @PathParam("idCardType") String idCardType, @PathParam("idCardNo") String idCardNo, @PathParam("createTimeStart") String createTimeStart, @PathParam("createTimeEnd") String createTimeEnd, @PathParam("updateTimeStart") String updateTimeStart, @PathParam("updateTimeEnd") String updateTimeEnd, @RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize, @RequestParam("systemSign") String systemSign) {
        CustomerInfoRo ro = new CustomerInfoRo();
        ro.setCustomerNo(customerNo);
        ro.setCustomerName(customerName);
        ro.setIdCardType(idCardType);
        ro.setIdCardNo(idCardNo);
        ro.setCreateTimeStart(createTimeStart);
        ro.setCreateTimeEnd(createTimeEnd);
        ro.setUpdateTimeStart(updateTimeStart);
        ro.setUpdateTimeEnd(updateTimeEnd);
        ro.setPageNo(pageNo);
        ro.setPageSize(pageSize);
        ro.setSystemSign(systemSign);
        if (StringUtils.isNotBlank(createTimeStart) && StringUtils.isNotBlank(createTimeEnd)) {
            if (createTimeStart.compareTo(createTimeEnd) >= 0) {
                return messageFactory.getInstance(CustomerCodes.PARAM_VALIDATE_ERROR, "结束时间必须大于起始时间");
            }
        }
        PageInfo<CustomerInfoVo> list = customerInfoService.selectAllCustomer(ro);
        Map<String, Object> pageInfo = new HashMap<>();
        pageInfo.put("pageInfo", list);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, pageInfo);
    }

    @GetMapping("/company")
    public Message getCompanyCustomer(@PathParam("customerNo") String customerNo, @PathParam("customerType") String customerType, @PathParam("customerName") String customerName, @PathParam("idCardType") String idCardType, @PathParam("orgNo") String orgNo, @PathParam("unifiedCode") String unifiedCode, @PathParam("createTimeStart") String createTimeStart, @PathParam("createTimeEnd") String createTimeEnd, @PathParam("updateTimeStart") String updateTimeStart, @PathParam("updateTimeEnd") String updateTimeEnd, @RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize, @RequestParam("systemSign") String systemSign) {
        CompanyCustomerInfoRo ro = new CompanyCustomerInfoRo();
        ro.setCustomerNo(customerNo);
        ro.setCustomerName(customerName);
        ro.setCustomerType(customerType);
        ro.setIdCardType(idCardType);
        ro.setUnifiedCode(unifiedCode);
        ro.setOrgNo(orgNo);
        ro.setCreateTimeStart(createTimeStart);
        ro.setCreateTimeEnd(createTimeEnd);
        ro.setUpdateTimeStart(updateTimeStart);
        ro.setUpdateTimeEnd(updateTimeEnd);
        ro.setPageNo(pageNo);
        ro.setPageSize(pageSize);

        // List<String> result = Splitter.on(",").trimResults().splitToList(str);
        ro.setSystemSign(systemSign);
        if (StringUtils.isNotBlank(createTimeStart) && StringUtils.isNotBlank(createTimeEnd)) {
            if (createTimeStart.compareTo(createTimeEnd) >= 0) {
                return messageFactory.getInstance(CustomerCodes.PARAM_VALIDATE_ERROR, "结束时间必须大于起始时间");
            }
        }
        PageInfo<CompanyCustomerInfoVo> list = customerInfoService.selectAllCompany(ro);
        Map<String, Object> pageInfo = new HashMap<>();
        pageInfo.put("pageInfo", list);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, pageInfo);
    }

    @GetMapping("/preCustomer/extend")
    public Message getPreExtend(@PathParam("preCustomerNo") String preCustomerNo) {
        Map<String, Object> map = customerInfoService.selectPreExtend(preCustomerNo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    @GetMapping("/preCustomer/change")
    public Message getPreChange(@PathParam("preCustomerNo") String preCustomerNo) {
        Map<String, Object> map = customerInfoService.selectPreChange(preCustomerNo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    @GetMapping("/preCustomer/static")
    public Message getPreStatic() {
        Map<String, Object> map = customerInfoService.selectPreStatic();
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    /**
     * @description: 个人客户详情
     * @author: denghx
     * @date: 2018/12/19 13:37
     * @param: customerNo
     * @return: com.credithc.baseserv.core.message.Message
     */
    @GetMapping("/customer/extend")
    public Message getCustomerExtend(@PathParam("customerNo") String customerNo) {
        Map<String, Object> map = customerInfoService.selectCustomerExtend(customerNo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    /**
     * @param customerName
     * @param idCardType
     * @param idCardNo
     * @description: 个人客户证件信息修改
     * @author: denghx
     * @date: 2019/1/15 9:57
     * @param: id
     * @return: com.credithc.baseserv.core.message.Message
     */
    @GetMapping("/customer/update")
    public Message update(@PathParam("id") String id, @PathParam("customerName") String customerName, @PathParam("idCardType") String idCardType, @PathParam("idCardNo") String idCardNo) {
        Map<String, Object> map = customerInfoService.updatePerson(id, customerName, idCardType, idCardNo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    @GetMapping("/customer/notice")
    public Message notice(@PathParam("id") String id, @PathParam("customerNo") String customerNo, @PathParam("customerName") String customerName, @PathParam("idCardType") String idCardType, @PathParam("idCardNo") String idCardNo) {
        Map<String, Object> map = customerInfoService.updatePersonNotice(id, customerNo, customerName, idCardType, idCardNo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    /**
     * 企业客户详情
     *
     * @description:
     * @author: denghx
     * @date: 2018/12/19 14:37
     * @param: customerNo
     * @return: com.credithc.baseserv.core.message.Message
     */
    @GetMapping("/company/extend")
    public Message getCompanyExtend(@PathParam("customerNo") String customerNo) {
        Map<String, Object> map = customerInfoService.selectCompanyExtend(customerNo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    /**
     * @param legalName
     * @param idCardType
     * @param idCardNo
     * @description: 对公客户证件信息修改
     * @author: denghx
     * @date: 2019/1/14 16:04
     * @param: id
     * @return: com.credithc.baseserv.core.message.Message
     */
    @GetMapping("/company/update")
    public Message updateCompany(@PathParam("id") String id, @PathParam("legalName") String legalName, @PathParam("idCardType") String idCardType, @PathParam("idCardNo") String idCardNo) {
        Map<String, Object> map = customerInfoService.updateCompany(id, legalName, idCardType, idCardNo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    @GetMapping("/company/notice")
    public Message updateCompanyNotice(@PathParam("id") String id, @PathParam("customerNo") String customerNo, @PathParam("legalName") String legalName, @PathParam("idCardType") String idCardType, @PathParam("idCardNo") String idCardNo) {
        Map<String, Object> map = customerInfoService.updateCompanyNotice(id, customerNo, legalName, idCardType, idCardNo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    /**
     * @param customerNo
     * @param mergeId
     * @param mergeCustomerNo
     * @description: 客户号合并处理
     * @author: denghx
     * @date: 2019/1/17 14:02
     * @param: id
     * @return: com.credithc.baseserv.core.message.Message
     */
    @GetMapping("/customer/merge")
    public Message merge(@PathParam("id") String id, @PathParam("customerNo") String customerNo, @PathParam("mergeId") String mergeId, @PathParam("mergeCustomerNo") String mergeCustomerNo) {
        Map<String, Object> map = customerInfoService.editMergePerson(id, customerNo, mergeId, mergeCustomerNo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    /**
     * @description: 业务信息变更合并处理
     * @author: denghx
     * @date: 2019/1/17 14:02
     * @param: id
     * @return: com.credithc.baseserv.core.message.Message
     */
    @GetMapping("/customer/getMergeBizInfo")
    public Message getMergeBizInfo(@PathParam("id") String id) {
        Map<String, Object> map = customerInfoService.editMergeBizInfo(id);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    @GetMapping("/customer/mergeBiz")
    public Message mergeBiz(@PathParam("id") String id, @PathParam("customerNo") String customerNo, @PathParam("mergeId") String mergeId, @PathParam("mergeCustomerNo") String mergeCustomerNo, @PathParam("bizId") String bizId) {
        Map<String, Object> map = customerInfoService.editMergeBizInfo(id, customerNo, mergeId, mergeCustomerNo, bizId);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    @GetMapping("/customer/sendBizNotice")
    public Message sendBizNotice(@PathParam("id") String id, @PathParam("customerNo") String customerNo, @PathParam("customerName") String customerName, @PathParam("idCardType") String idCardType, @PathParam("idCardNo") String idCardNo, @PathParam("type") Integer type) {
        Map<String, Object> map = customerInfoService.editBizNotice(id, customerNo, customerName, idCardType, idCardNo, type);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

}
